package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.FieldValOptionVo;
import com.zb.entity.ZjNetworkEmpAuthenFieldVal;
import com.zb.entity.ZjNetworkEmpAuthenFieldValOption;
import com.zb.extend.NetworkEmpAuthenFieldValExtend;
import com.zb.mapper.ZjNetworkEmpAuthenFieldValMapper;
import com.zb.service.ZjNetworkEmpAuthenFieldValOptionService;
import com.zb.service.ZjNetworkEmpAuthenFieldValService;
import com.zb.vo.NetworkFieldValVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 网点人员认证字段值表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-02-01
 */
@Service
public class ZjNetworkEmpAuthenFieldValServiceImpl extends ServiceImpl<ZjNetworkEmpAuthenFieldValMapper, ZjNetworkEmpAuthenFieldVal> implements ZjNetworkEmpAuthenFieldValService {
    @Resource
    private ZjNetworkEmpAuthenFieldValOptionService networkEmpAuthenFieldValOptionService;

    // 根据商家ID & 工程师人员ID获取认证字段
    @Override
    public List<NetworkEmpAuthenFieldValExtend> getByCustAccIdAndNetworkEmpId(Long custAccId, Long networkEmpId) {
        QueryWrapper<ZjNetworkEmpAuthenFieldVal> wrapper = new QueryWrapper<>();
        wrapper.select("id", "network_emp_id", "network_emp_authen_field_id", "type")
                .eq("cust_acc_id", custAccId).eq("network_emp_id", networkEmpId);
        List<ZjNetworkEmpAuthenFieldVal> fieldValList = this.list(wrapper);
        if (CollectionUtil.isEmpty(fieldValList)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjNetworkEmpAuthenFieldValOption> valOptionQueryWrapper = new QueryWrapper<>();
        valOptionQueryWrapper.select("id", "opt_value")
                .in("network_emp_authen_field_val_id", fieldValList.stream().map(ZjNetworkEmpAuthenFieldVal::getId).toList());
        List<ZjNetworkEmpAuthenFieldValOption> valOptionList = networkEmpAuthenFieldValOptionService.list(valOptionQueryWrapper);
        return fieldValList.stream().map(val -> {
            NetworkEmpAuthenFieldValExtend extend = new NetworkEmpAuthenFieldValExtend();
            BeanUtil.copyProperties(val, extend);
            extend.setValues(valOptionList.stream().filter(f -> Objects.equals(f.getNetworkEmpAuthenFieldValId(), val.getId())).map(ZjNetworkEmpAuthenFieldValOption::getOptValue).toList());
            return extend;
        }).toList();
    }

    // 保存工程师认证字段
    @Override
    public void save(Long custAccId, Long networkEmpId, List<NetworkFieldValVo> authorFieldVals) {
        if (CollectionUtil.isEmpty(authorFieldVals)) {
            return;
        }
        List<ZjNetworkEmpAuthenFieldVal> adds = new ArrayList<>();
        List<ZjNetworkEmpAuthenFieldVal> updates = new ArrayList<>();
        ZjNetworkEmpAuthenFieldVal authenFieldVal = null;
        List<ZjNetworkEmpAuthenFieldValOption> valOptionList = new ArrayList<>();
        ZjNetworkEmpAuthenFieldValOption valOption = null;
        for (NetworkFieldValVo vo : authorFieldVals) {
            authenFieldVal = new ZjNetworkEmpAuthenFieldVal();
            BeanUtil.copyProperties(vo, authenFieldVal);
            if (null != authenFieldVal.getId()) {
                updates.add(authenFieldVal);
            } else {
                authenFieldVal.setId(IdWorker.getId());
                authenFieldVal.setCustAccId(custAccId);
                authenFieldVal.setNetworkEmpId(networkEmpId);
                authenFieldVal.setNetworkEmpAuthenFieldId(vo.getFieldId());
                adds.add(authenFieldVal);
            }
            for (FieldValOptionVo optVo : vo.getOptVos()) {
                valOption = new ZjNetworkEmpAuthenFieldValOption();
                valOption.setId(IdWorker.getId());
                valOption.setCustAccId(custAccId);
                valOption.setNetworkEmpId(networkEmpId);
                valOption.setNetworkEmpAuthenFieldValId(authenFieldVal.getId());
                valOption.setOptValue(optVo.getOptValue());
                valOptionList.add(valOption);
            }
        }
        if (CollectionUtil.isNotEmpty(adds)) this.saveBatch(adds);
        if (CollectionUtil.isNotEmpty(updates)) this.updateBatchById(updates);
        networkEmpAuthenFieldValOptionService.deleteByCustAccIdAndNeworkEmpId(custAccId, networkEmpId);
        networkEmpAuthenFieldValOptionService.saveBatch(valOptionList);
    }
}
